package com.kpx.app.utils;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.kpx.app.Application;
import com.kpx.app.R;
import com.kpx.app.activity.ActivityBuyMyself;
import com.kpx.app.activity.ActivityOrderDetail;
import com.kpx.app.activity.ActivityOrderList;
import com.kpx.app.activity.ActivityOrderListByStatus;
import com.kpx.app.activity.ActivityOrderPrise;
import com.kpx.app.activity.ActivityOrderUploadPics;
import com.kpx.app.activity.ActivityPayList;
import com.kpx.app.activity.ActivitySetOrderInfo;
import com.kpx.app.activity.ActivitySetWuliuInfo;
import com.kpx.app.activity.ActivityUploadManager;
import com.kpx.app.activity.ActivityWuliuInfoShow;
import com.kpx.app.base.ActivityBase;
import com.kpx.app.base.ComViewHolder;
import com.kpx.app.bean.LoginUser;
import com.kpx.app.bean.OrderBean;
import com.kpx.app.bean.UploadBean;
import com.kpx.app.cache.FileCache;
import com.kpx.app.configs.BroadcastFilters;
import com.kpx.app.configs.Constants;
import com.kpx.app.http.HttpDataApi;
import com.kpx.app.http.HttpDataLoader;
import com.kpx.app.http.HttpDataReqUrl;
import com.kpx.app.http.ResObj;
import com.kpx.app.utils.uiutils.IntentUtil;
import com.kpx.app.utils.uiutils.ToastUtil;
import com.kpx.app.wigdet.DialogFactoryImpl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.finalteam.galleryfinal.GalleryFinal;
import cn.finalteam.galleryfinal.model.PhotoInfo;


/**
 * Created by congdy on 17/4/16.
 */

public class OrderUtils {
    private static final int BTN_GREY = 1;
    private static final int BTN_RED = 2;

    public static void setBtnStatus(final Context context, final OrderBean b, Button btn1, Button btn2, Button btn3, TextView txtStatus) {
        btn1.setOnClickListener(null);
        btn2.setOnClickListener(null);
        btn3.setOnClickListener(null);
        switch (b.getOrderStatus()) {
            case OrderBean.ORDER_STATUS_COMPLETED_WATTING_PRICE://已完成-->评价
                if (b.getModelAppraise() == 0) {
                    setOrderStatus(txtStatus, "已完成");
                    setBtnStyle(context, btn2, BTN_RED, "去评价");
                    btn2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Bundle bundle = new Bundle();
                            bundle.putSerializable(Constants.KEY_ORDER_BEAN, b);
                            IntentUtil.gotoActivity(context, ActivityOrderPrise.class, bundle);
                        }
                    });
                } else {
                    setOrderStatus(txtStatus, "已评价");
                }
                break;
            case OrderBean.ORDER_STATUS_COMPLETED_HAD_PRICE://已完成-->查看评价
                setBtnStyle(context, btn1, BTN_GREY, "查看评价");
                setOrderStatus(txtStatus, "已完成");
                btn1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
                break;
            case OrderBean.ORDER_STATUS_WAITTING_GET://待收货-->查看物流，确认收货
                setOrderStatus(txtStatus, "待收货");
                setBtnStyle(context, btn1, BTN_GREY, "查看物流");
                btn1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Bundle bundle = new Bundle();
                        bundle.putSerializable(Constants.KEY_ORDER_BEAN, b);
                        IntentUtil.gotoActivity(context, ActivityWuliuInfoShow.class, bundle);
                    }
                });
                setBtnStyle(context, btn2, BTN_RED, "确认收货");
                btn2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DialogFactoryImpl.newInstance(context).showConfirmDialog("取消", "确定", "确定收货吗", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                switch (which) {
                                    case DialogInterface.BUTTON_NEGATIVE:
                                        break;
                                    case DialogInterface.BUTTON_POSITIVE:
                                        toDoOrder(b, context);
                                        break;
                                }
                            }
                        });

                    }
                });
                break;
            case OrderBean.ORDER_STATUS_WAITTING_SEND://待发货-->提醒商家发货
                setOrderStatus(txtStatus, "待发货");
                setBtnStyle(context, btn2, BTN_RED, "提醒商家发货");
                btn2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        toDoOrder(b, context);
                    }
                });
                break;
            case OrderBean.ORDER_STATUS_WAITTING_PAY://待支付-->取消订单，去支付
                setOrderStatus(txtStatus, "待支付");
                setBtnStyle(context, btn1, BTN_GREY, "取消订单");
                btn1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DialogFactoryImpl.newInstance(context).showConfirmDialog("取消", "确定", "确定取消订单吗", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                switch (which) {
                                    case DialogInterface.BUTTON_NEGATIVE:
                                        break;
                                    case DialogInterface.BUTTON_POSITIVE:
                                        toDoOrder(b, context);
                                        break;
                                }
                            }
                        });

                    }
                });
                setBtnStyle(context, btn2, BTN_RED, "去支付");
                btn2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Bundle bundle = new Bundle();
                        bundle.putSerializable(Constants.KEY_ORDER_BEAN, b);
                        IntentUtil.gotoActivity(context, ActivityPayList.class, bundle);
                    }
                });
                break;
            case OrderBean.ORDER_STATUS_WAITTING_UPLOAD://待上传-->上传图片
                setOrderStatus(txtStatus, "待上传");
                setUpdatePic("上传图片", context, b, btn1, btn2, txtStatus);
                break;

            case OrderBean.ORDER_STATUS_WATTING_BACK://待退回商品-->上传物流信息
                setOrderStatus(txtStatus, "待退回商品");
                setBtnStyle(context, btn2, BTN_RED, "上传物流信息");
                btn2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Bundle bundle = new Bundle();
                        bundle.putSerializable(Constants.KEY_ORDER_BEAN, b);
                        IntentUtil.gotoActivity(context, ActivitySetWuliuInfo.class, bundle);
                    }
                });
                setBtnStyle(context, btn1, BTN_RED, "我要自购");
                btn1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Bundle bundle = new Bundle();
                        bundle.putSerializable(Constants.KEY_ORDER_BEAN,b);
                        IntentUtil.gotoActivity(context, ActivityBuyMyself.class,bundle);
                    }
                });
                break;
            case OrderBean.ORDER_STATUS_CLOSED://已关闭-->查看订单
                setOrderStatus(txtStatus, "已关闭");
                if (context instanceof ActivityOrderDetail) {
                    ((ActivityOrderDetail) context).findViewById(R.id.ll_btn).setVisibility(View.GONE);
                    return;
                }
                setBtnStyle(context, btn2, BTN_GREY, "查看订单");
                btn2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Bundle bundle = new Bundle();
                        bundle.putSerializable(Constants.KEY_ORDER_BEAN, b);
                        ActivityOrderDetail.inType = 1;
                        IntentUtil.gotoActivity(context, ActivityOrderDetail.class, bundle);
                    }
                });
                break;
            case OrderBean.ORDER_STATUS_WAITTING_FILL://待填写-->待模特填写第三方的交易信息
                toCommitAgain(context, b, btn1, btn2, btn3, txtStatus);
                break;
            case OrderBean.ORDER_STATUS_SELLER_REFUESED://商家拒绝-->商家拒绝，必须填写拒绝信息
                setOrderStatus(txtStatus, "待审核");
                setBtnStyle(context, btn2, BTN_RED, "提醒商家审核");
                btn2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        toDoOrder(b, context);
                    }
                });
                break;
            case OrderBean.ORDER_STATUS_WAITTING_SELLER_CHECK://待商家审核-->待平台审核
                setOrderStatus(txtStatus, "待审核");
                setBtnStyle(context, btn2, BTN_RED, "提醒商家审核");
                btn2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        toDoOrder(b, context);
                    }
                });
                break;
            case OrderBean.ORDER_STATUS_WATTING_CHECK://待商家审核-->待平台审核
                setOrderStatus(txtStatus, "待审核");
                setBtnStyle(context, btn2, BTN_RED, "提醒商家审核");
                btn2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        toDoOrder(b, context);
                    }
                });
                break;
            case OrderBean.ORDER_STATUS_WATTING_SURE://待商家确认-->模特已退货，待商家确认
                setOrderStatus(txtStatus, "待商家确认");
                break;
            case OrderBean.ORDER_STATUS_WATTING_TUIKUAN://待退款-->商家确认收到退货，待平台退还模特担保金
                setOrderStatus(txtStatus, "待退款");
                break;
            case OrderBean.ORDER_STATUS_MODEL_BUY://模特自购-->模特自购，待商家确认
                setOrderStatus(txtStatus, "自购待商家确认");
                break;
            case OrderBean.ORDER_STATUS_REFUESD_BY_PINTAI://平台审核不通过
                setOrderStatus(txtStatus, "平台审核不通过");
                setUpdatePic("重新上传", context, b, btn1, btn2, txtStatus);
                break;
            case OrderBean.ORDER_STATUS_REFUESD_BY_SELLER://商家审核不通过
                setOrderStatus(txtStatus, "商家审核不通过");
                setUpdatePic("重新上传", context, b, btn1, btn2, txtStatus);
                break;
            case OrderBean.ORDER_STATUS_WATTING_SP://待收评
                setOrderStatus(txtStatus, "待收货晒图评价");
                setBtnStyle(context, btn2, BTN_RED, "确认已收评");
                btn2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        toDoOrder(b, context);
                    }
                });
                break;
            case OrderBean.ORDER_STATUS_SURE_SP://待收评审核
                setOrderStatus(txtStatus, "待收评审核");
                setBtnStyle(context, btn2, BTN_RED, "提醒商家审核");
                btn2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        toDoOrder(b, context);
                    }
                });
                break;
        }
    }

    /**
     * 刷拍重新提交
     *
     * @param context
     * @param b
     * @param btn2
     * @param btn3
     * @param txtStatus
     */
    private static void toCommitAgain(final Context context, final OrderBean b, Button btn1, Button btn2, Button btn3, TextView txtStatus) {
        switch (b.getThirdOrderInfoStatus()) {
            case 0:
                flowFirst(context, b, btn1, btn2, btn3, txtStatus);
                break;
            case 1:
                setOrderStatus(txtStatus, "商家审核通过");
                break;
            case 2:
                setOrderStatus(txtStatus, "商家审核不通过");
                setBtnStyle(context, btn3, BTN_GREY, "取消订单");
                btn3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DialogFactoryImpl.newInstance(context).showConfirmDialog("取消", "确定", "确定取消订单吗", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                switch (which) {
                                    case DialogInterface.BUTTON_NEGATIVE:
                                        break;
                                    case DialogInterface.BUTTON_POSITIVE:
                                        toDoOrder(b, context);
                                        break;
                                }
                            }
                        });

                    }
                });
                setBtnStyle(context, btn2, BTN_RED, "提交订单号");
                btn2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Bundle bundle = new Bundle();
                        bundle.putSerializable(Constants.KEY_ORDER_BEAN, b);
                        IntentUtil.gotoActivity(context, ActivitySetOrderInfo.class, bundle);
                    }
                });
                break;
            default:
                flowFirst(context, b, btn1, btn2, btn3, txtStatus);
                break;
        }
    }

    private static void flowFirst(final Context context, final OrderBean b, Button btn1, Button btn2, Button btn3, TextView txtStatus) {
        setOrderStatus(txtStatus, "待填写");
        setBtnStyle(context, btn3, BTN_GREY, "取消订单");
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogFactoryImpl.newInstance(context).showConfirmDialog("取消", "确定", "确定取消订单吗", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        switch (which) {
                            case DialogInterface.BUTTON_NEGATIVE:
                                break;
                            case DialogInterface.BUTTON_POSITIVE:
                                toDoOrder(b, context);
                                break;
                        }
                    }
                });

            }
        });
        setBtnStyle(context, btn1, BTN_RED, "去下单");
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (b.getThridPlatform()) {//1.淘宝，2.天猫，3、京东
                    case 3:
                        UIUtils.goJd(context, b.getThridPlatformId(),(b.getTaokoulingStr()==null|| TextUtils.isEmpty(b.getTaokoulingStr()))?(b.getTaskGoods()==null?"":b.getTaskGoods().getGoodsTitle()):b.getTaokoulingStr());
                        break;
                    case 1:
                        UIUtils.gotb(context, b.getThridPlatformId(),(b.getTaokoulingStr()==null|| TextUtils.isEmpty(b.getTaokoulingStr()))?(b.getTaskGoods()==null?"":b.getTaskGoods().getGoodsTitle()):b.getTaokoulingStr());
                        break;
                    case 2:
                        UIUtils.gotm(context, b.getThridPlatformId(),(b.getTaokoulingStr()==null|| TextUtils.isEmpty(b.getTaokoulingStr()))?(b.getTaskGoods()==null?"":b.getTaskGoods().getGoodsTitle()):b.getTaokoulingStr());
                        break;
                }
            }
        });
        setBtnStyle(context, btn2, BTN_RED, "提交订单号");
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putSerializable(Constants.KEY_ORDER_BEAN, b);
                IntentUtil.gotoActivity(context, ActivitySetOrderInfo.class, bundle);
            }
        });
    }

    private static void setUpdatePic(final String str, final Context context, final OrderBean b, Button btn1, Button btn2, TextView txtStatus) {
        if (b.getNowPhotosNum() < b.getMaxPhotosNum()) {
            setBtnStyle(context, btn2, BTN_RED, str);
            btn2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(Constants.KEY_ORDER_BEAN, b);
                    IntentUtil.gotoActivity(context, ActivityOrderUploadPics.class, bundle);
                }
            });
        }
        if (b.getNowPhotosNum() >= b.getMinPhotosNum()) {
            setBtnStyle(context, btn1, BTN_RED, "确认上传完成");
            btn1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DialogFactoryImpl.newInstance(context).showConfirmDialog("取消", "确定", "确定上传完成吗", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            switch (which) {
                                case DialogInterface.BUTTON_NEGATIVE:
                                    break;
                                case DialogInterface.BUTTON_POSITIVE:
                                    toDoOrder(b, context);
                                    break;
                            }
                        }
                    });
                }
            });
        }
    }

    private static void setOrderStatus(TextView txtStatus, String str) {
        if (txtStatus != null) {
            txtStatus.setText(str);
        }
    }

    public static void toDoOrder(final OrderBean b, final Context mContext) {
        showLoadingDialog(true, mContext);
        String url = "";
        final MyHashMap map = new MyHashMap();
        //handleType 1:提醒发货,2：提醒审核,3 提醒商家审核模特填写的第三方信息
        switch (b.getOrderStatus()) {
            case OrderBean.ORDER_STATUS_WAITTING_PAY://待支付-->取消订单
            case OrderBean.ORDER_STATUS_WAITTING_FILL://待支付-->取消订单
                url = HttpDataReqUrl.URL_POST_POST_TO_CANCEL;
                break;
            case OrderBean.ORDER_STATUS_WAITTING_GET://待收货-->查看物流，确认收货
                url = HttpDataReqUrl.URL_POST_POST_TO_SURE;
                break;
            case OrderBean.ORDER_STATUS_WAITTING_UPLOAD://待上传-->上传图片
            case OrderBean.ORDER_STATUS_REFUESD_BY_PINTAI://平台审核不通过
            case OrderBean.ORDER_STATUS_REFUESD_BY_SELLER://商家审核不通过
                url = HttpDataReqUrl.URL_POST_ORDER_UPLOAD_COMPLETE;
                break;
            case OrderBean.ORDER_STATUS_WATTING_BACK://待退回-->自购
                url = HttpDataReqUrl.URL_POST_TO_BUY_GOODS;
                break;
            case OrderBean.ORDER_STATUS_WATTING_SP://收评
                url = HttpDataReqUrl.URL_POST_GET_SURE_SP;
                break;
            case OrderBean.ORDER_STATUS_WAITTING_SEND://待发货-->提醒商家发货
                url = HttpDataReqUrl.URL_POST_POST_TO_SEND_OR_CHECK;
                map.put("handleType", "1");
                break;
            case OrderBean.ORDER_STATUS_WAITTING_SELLER_CHECK://发审核-->提醒商家审核
            case OrderBean.ORDER_STATUS_WATTING_CHECK://发审核-->提醒商家审核
                url = HttpDataReqUrl.URL_POST_POST_TO_SEND_OR_CHECK;
                map.put("handleType", "2");
                break;
            case OrderBean.ORDER_STATUS_SELLER_REFUESED://发审核-->提醒商家审核
                url = HttpDataReqUrl.URL_POST_POST_TO_SEND_OR_CHECK;
                map.put("handleType", "3");
                break;
            case OrderBean.ORDER_STATUS_SURE_SP://提醒商家审核
                url = HttpDataReqUrl.URL_POST_POST_TO_SEND_OR_CHECK;
                map.put("handleType", "4");
                break;
        }
        map.put("orderId", b.getDmId() + "");
        new HttpDataLoader(new HttpDataApi.OnRequestCallback() {
            @Override
            public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {
                showLoadingDialog(false, mContext);
                if (result.getCode() == 0) {
                    switch (b.getOrderStatus()) {
                        case OrderBean.ORDER_STATUS_WAITTING_PAY://待支付-->取消订单
                        case OrderBean.ORDER_STATUS_WAITTING_FILL://待支付-->取消订单
                            ToastUtil.getInstance().showToast(mContext, "取消成功");
                            break;
                        case OrderBean.ORDER_STATUS_WAITTING_GET://待收货-->查看物流，确认收货
                            ToastUtil.getInstance().showToast(mContext, "操作成功");
                            break;
                        case OrderBean.ORDER_STATUS_WAITTING_SEND://待发货-->提醒商家发货
                            ToastUtil.getInstance().showToast(mContext, "已提醒商家发货");
                            break;
                        case OrderBean.ORDER_STATUS_WAITTING_SELLER_CHECK://待审核-->提醒商家审核
                        case OrderBean.ORDER_STATUS_SELLER_REFUESED://待审核-->提醒商家审核
                        case OrderBean.ORDER_STATUS_WATTING_CHECK://待审核-->提醒商家审核
                        case OrderBean.ORDER_STATUS_SURE_SP://待审核-->提醒商家审核
                            ToastUtil.getInstance().showToast(mContext, "已提醒商家审核");
                            break;
                        case OrderBean.ORDER_STATUS_WATTING_BACK://待退回-->自购
                            ToastUtil.getInstance().showToast(mContext, "自购成功");
                            if(mContext instanceof ActivityOrderDetail){
                                ((ActivityOrderDetail)mContext).finish();
                            }
                            break;
                        case OrderBean.ORDER_STATUS_WATTING_SP://待收评
                            if(mContext instanceof ActivityOrderDetail){
                                ((ActivityOrderDetail)mContext).finish();
                            }
                            ToastUtil.getInstance().showToast(mContext, "已确认收评");
                            break;
                    }
                    mContext.sendBroadcast(new Intent(BroadcastFilters.BROADCAST_ACTION_REFRESH_DATA));
                } else {
                    ToastUtil.getInstance().showToast(mContext, result.getMsg());
                }
            }

            @Override
            public void onRequestError(String url, int statusCode, String error) {
                showLoadingDialog(false, mContext);
                ToastUtil.getInstance().showToast(mContext, error);
            }

        }, null, null).POST(url, true, map);

    }

    public static void showLoadingDialog(boolean b, Context mContext) {
        if (b) {
            ((ActivityBase) mContext).showLoadingDialog();
        } else {
            ((ActivityBase) mContext).dissmissLoadingDialog();
        }
    }

    public static void setBtnStyle(final Context context, Button btn, int style, String str) {
        switch (style) {
            case BTN_GREY:
                if (context instanceof ActivityOrderList || context instanceof ActivityOrderListByStatus) {
                    btn.setVisibility(View.VISIBLE);
                    btn.setTextColor(context.getResources().getColor(R.color.grey_b4));
                    btn.setBackgroundResource(R.drawable.btn_order_style_grey);
                    btn.setText(str);
                } else if (context instanceof ActivityOrderDetail) {
                    btn.setVisibility(View.VISIBLE);
                    btn.setTextColor(context.getResources().getColor(R.color.black_51));
                    btn.setBackgroundResource(R.drawable.btn_red_stoke_shape);
                    btn.setText(str);
                }
                break;
            case BTN_RED:
                if (context instanceof ActivityOrderList || context instanceof ActivityOrderListByStatus) {
                    btn.setVisibility(View.VISIBLE);
                    btn.setTextColor(context.getResources().getColor(R.color.red_FF4088));
                    btn.setBackgroundResource(R.drawable.btn_red_stoke_shape);
                    btn.setText(str);
                } else if (context instanceof ActivityOrderDetail) {
                    btn.setVisibility(View.VISIBLE);
                    btn.setTextColor(context.getResources().getColor(R.color.white));
                    btn.setBackgroundResource(R.drawable.btn_theme_bg2);
                    btn.setText(str);
                }

                break;
        }
    }
}
